package com.cd.javacxy.module.base;


import com.cd.javacxy.common.model.entity.SessionUser;
import com.cd.javacxy.common.utils.Constants;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;


/**
 * @author cookie
 */
public abstract class BaseController {

    protected void setUserBaseInfo(Class<?> clazz, Object obj, HttpSession session) {
        SessionUser sessionUser = (SessionUser) session.getAttribute(Constants.SESSION_USER_KEY);
     /*   Integer userId = 10000;
        String userName = "官方小编";
        String userIcon = "user_icon/10000.jpg";*/
        Integer userId = sessionUser.getUserId();
        String userName = sessionUser.getUserName();
        String userIcon = sessionUser.getUserIcon();
        try {
            Method userIdMethod = clazz.getDeclaredMethod("setUserId", Integer.class);
            userIdMethod.invoke(obj, userId);
            Method userNameMethod = clazz.getDeclaredMethod("setUserName", String.class);
            userNameMethod.invoke(obj, userName);
            Method userIconMethod = clazz.getDeclaredMethod("setUserIcon", String.class);
            userIconMethod.invoke(obj, userIcon);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    protected Integer getUserId(HttpSession session) {
        Object sessionObject = session.getAttribute(Constants.SESSION_USER_KEY);
        if (sessionObject != null) {
            return ((SessionUser) sessionObject).getUserId();
        }
        return null;
    }
}
